package org.zttc.itat.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zttc.itat.model.Sort;
import org.zttc.itat.model.SortType;
import org.zttc.itat.service.ISortService;
import org.zttc.itat.service.ISortTypeService;
import org.zttc.itat.util.ActionUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller("sortAction")
@Scope("prototype")
public class SortAction extends ActionSupport implements ModelDriven<Sort>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Sort sort;
	private int sortId;
	private SortType sortType;
	@Resource(name="sortService")
	private ISortService sortService;
	@Resource(name="sortTypeService")
	private ISortTypeService sortTypeService;
	@Override
	public Sort getModel() {
        if(sort==null) sort = new Sort();
		return sort;
	}
	
	public Sort getSort() {
		return sort;
	}
	public void setSort(Sort sort) {
		this.sort = sort;
	}
	public int getSortId() {
		return sortId;
	}
	public void setSortId(int sortId) {
		this.sortId = sortId;
	}
   
	public SortType getSortType() {
		return sortType;
	}

	public void setSortType(SortType sortType) {
		this.sortType = sortType;
	}

	public String add(){
		sortService.add(sort);
		ActionUtil.setUrl("/later_sort_list.action");
		return ActionUtil.REDIRECT;
	}
	
	public void validateAdd() {
		if(ActionUtil.isEmpty(sort.getSortname())) {
			this.addFieldError("sortname","栏目不能为空");
		}if(ActionUtil.isEmpty(sort.getEnglishfield())){
			this.addFieldError("englishfield","栏目英文字段不能为空");
		}
		if(this.hasErrors()) addInput();
	}
	
	public String update(){
		Sort st = sortService.load(sort.getId());
		st.setSortname(sort.getSortname());
		sortService.update(st);
		ActionUtil.setUrl("/later_sort_list.action");
		return ActionUtil.REDIRECT;
	}
	
	public void validateUpdate() {
		if(ActionUtil.isEmpty(sort.getSortname())){
			this.addFieldError("sortname","栏目不能为空");
		}if(ActionUtil.isEmpty(sort.getEnglishfield())){
			this.addFieldError("englishfield","栏目英文字段不能为空");
		}
		if(this.hasErrors()) updateInput();
	}
	
	public String updateInput() {
		sort = sortService.load(sort.getId());
		return SUCCESS;
	}
	
	public String addInput(){
		return SUCCESS;
	}
	
	public String show(){
		sort = sortService.load(sort.getId());
		ActionContext.getContext().put("st",sortService.listScopeSort(sort.getId()));
		return SUCCESS;
	}
	
	public String list(){
		List<Sort> ds = sortService.listAllSort();
		ActionContext.getContext().put("ds",ds);
		return SUCCESS;
	}
	
	public String delete(){
		sortService.delete(sort.getId());
		ActionUtil.setUrl("/later_sort_list.action");
		return ActionUtil.REDIRECT;
	}
	
	public String addSortTypeInput(){
		sort= sortService.load(sortId);
		ActionContext.getContext().put("ds",sortService.listAllSort());
		return SUCCESS;
	}
	
	public String addSortType(){
		   //1、添加二级栏目
		SortType st =sortTypeService.load(sortType.getEnglishfield());
		if(st==null){
			sortType.setName(sortType.getName());
			sortType.setEnglishfield(sortType.getEnglishfield());
			sortTypeService.add(sortType);
			st = sortTypeService.load(sortType.getEnglishfield());
		}
		sortService.addTypeSort(sortId,st.getId());
		ActionUtil.setUrl("/later_sort_list.action");
		return ActionUtil.REDIRECT;
	}
}
